﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.PPSM2007;
using JM.JCY.Proxy;
using Microsoft.SharePoint;
using JM.JCY.BusinessEntity;
using System.Linq;
using System.Data;
using System.IO;

namespace JM.JCY.SharePointComponent.PersonalInformationDocument.EducationAndTrainingRecord
{
    public partial class EducationAndTrainingRecordUserControl : UserControl
    {
        PPSM2007CodeDataSet.T_PPSM2007CODEDataTable CodeTable;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CodeTable = GetPPSM2007CODEData();
                //SPWeb web = SPContext.Current.Web;

                var currentUserName = SPContext.Current.Web.CurrentUser.Name;
                //web.CurrentUser.Name;

               
                DataRow[] rows = GetEduInfoByName(currentUserName).Select();
                dlEduction.DataSource = rows;
                dlEduction.DataBind();
                if (rows.Count() > 0)
                {
                    
                    EducationNoRecord.Visible = false;
                }
                   
                else
                    EducationNoRecord.Visible = true;
                

                DataRow[] rows2 = GetTrainInfoByName(currentUserName).Select();
                dlTrainingRecord.DataSource = rows2;
                dlTrainingRecord.DataBind();
                if (rows2.Count() > 0)
                {
                    
                    TrainingNoRecord.Visible = false;
                }
                  
                else
                    TrainingNoRecord.Visible = true;
               
               
            }
        }

        public PPSMUserEduInfoDataSet.UserEduInfoDataTable GetEduInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetEduInfoByName(userName);
            }
        }

        public PPSMUserEduInfoDataSet.UserTrainDataTable GetTrainInfoByName(string userName)
        {
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetTrainInfoByName(userName);
            }
        }
        protected void dlTrainingRecord_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserEduInfoDataSet.UserTrainRow row = (PPSMUserEduInfoDataSet.UserTrainRow)e.Item.DataItem;
            Label lbStartDateTime = (Label)e.Item.FindControl("lbStartDateTime");
            Label lbEndDateTime = (Label)e.Item.FindControl("lbEndDateTime");
            Label lbTrainingType = (Label)e.Item.FindControl("lbTrainingType");
            Label lbTrainingContent = (Label)e.Item.FindControl("lbTrainingContent");
            Label lbTrainingPlace = (Label)e.Item.FindControl("lbTrainingPlace");
            lbStartDateTime.Text = string.IsNullOrEmpty(row.TrainBeginDate) ? string.Empty : row.TrainBeginDate.Substring(0, 4).ToString() + "年" + row.TrainBeginDate.Substring(4, 2).ToString() + "月" + row.TrainBeginDate.Substring(6, 2).ToString() + "日";
            lbEndDateTime.Text = string.IsNullOrEmpty(row.TrainEndDate) ? string.Empty : row.TrainEndDate.Substring(0, 4).ToString() + "年" + row.TrainEndDate.Substring(4, 2).ToString() + "月" + row.TrainEndDate.Substring(6, 2).ToString() + "日";
            lbTrainingType.Text = string.IsNullOrEmpty(row.TrainSort) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训类别", row.TrainSort);
            if (row.TrainSort == "50" || row.TrainSort == "90")
            {
                lbTrainingContent.Text = string.IsNullOrEmpty(row.TrainMatter) ? string.Empty : row.TrainMatter;
            }
            else
            {
                lbTrainingContent.Text = string.IsNullOrEmpty(row.TrainMatter) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训内容", row.TrainMatter);
            }
            lbTrainingPlace.Text = string.IsNullOrEmpty(row.TrainLevel) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "培训机构", row.TrainLevel);
        
        }

        protected void dlEduction_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            PPSMUserEduInfoDataSet.UserEduInfoRow row = (PPSMUserEduInfoDataSet.UserEduInfoRow)e.Item.DataItem;
            Label lbEducationType = (Label)e.Item.FindControl("lbEducationType");
            Label lbSchoolName = (Label)e.Item.FindControl("lbSchoolName");
            Label lbProfessionalName = (Label)e.Item.FindControl("lbProfessionalName");
            Label lbNationalEducationType = (Label)e.Item.FindControl("lbNationalEducationType");
            Label lbNationalEducationalBackground = (Label)e.Item.FindControl("lbNationalEducationalBackground");
            Label lbNationalEducationalLevel = (Label)e.Item.FindControl("lbNationalEducationalLevel");
            Label lbPartyEducationalBackground = (Label)e.Item.FindControl("lbPartyEducationalBackground");
            lbEducationType.Text = string.IsNullOrEmpty(row.UserEduArea) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "教育类型", row.UserEduArea);
            lbSchoolName.Text = string.IsNullOrEmpty(row.UserUniversityName) ? string.Empty : row.UserUniversityName;
            lbProfessionalName.Text = string.IsNullOrEmpty(row.UserSpecialtyName) ? string.Empty : row.UserSpecialtyName;
            lbNationalEducationType.Text = string.IsNullOrEmpty(row.EducationSort) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育类别", row.EducationSort);
            lbNationalEducationalBackground.Text = string.IsNullOrEmpty(row.UserGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育学历", row.UserGraduate);
            lbNationalEducationalLevel.Text = string.IsNullOrEmpty(row.UserDegree) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "国民教育学位", row.UserDegree);
            lbPartyEducationalBackground.Text = string.IsNullOrEmpty(row.UserPartyGraduate) ? string.Empty : GetCodeDescByCategoryAndValue(CodeTable, "党校学历", row.UserPartyGraduate);
        }

        public string GetCodeDescByCategoryAndValue(PPSM2007CodeDataSet.T_PPSM2007CODEDataTable dt, string category, string value)
        {
            try
            {
                var codeDesc = from codeTable in dt
                               where codeTable.Category == category && codeTable.CodeValue == value
                               select codeTable.CodeDesc;
                return codeDesc.First();
            }
            catch (Exception)
            {

                return "";
            }
          


        }

        public PPSM2007CodeDataSet.T_PPSM2007CODEDataTable GetPPSM2007CODEData()
        {
            var dt = new PPSM2007CodeDataSet.T_PPSM2007CODEDataTable();
            using (PPSM2007Proxy proxy = new PPSM2007Proxy())
            {
                return proxy.GetPPSM2007CodeTable();
            }
        }
    }
}
